package com.site.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.site.action.AdvertiseAction;
import com.site.dao.AdvertiseDao;
import com.site.dto.AdvertiseBizBean;
import com.site.model.AdvertiseInfo;
import com.site.service.AdvertiseService;
import com.site.util.DateUtils;
import com.site.util.PropertyUtils;
import com.site.util.base.businessbean.FileBean;
import com.site.util.base.service.BaseServiceImpl;
import com.site.util.constant.Constants;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

/**
 * 广告
 * @author lee
 * @date 2013年11月30日
 */
@Component
public class AdvertiseServiceImpl extends BaseServiceImpl implements AdvertiseService {

	private AdvertiseDao advertiseDao;
	
	@Resource
	public void setAdvertiseDao(AdvertiseDao advertiseDao) {
		this.advertiseDao = advertiseDao;
	}

	@Override
	public List<AdvertiseBizBean> getAdvertiseList(QueryPage queryPage,	AdvertiseBizBean advertiseBizBean) {
		List<AdvertiseInfo> advertiseInfos = advertiseDao.getAdvertiseList(queryPage, advertiseBizBean);
		if(null != advertiseInfos && advertiseInfos.size() > 0){
			List<AdvertiseBizBean> advertiseBizBeans = new ArrayList<AdvertiseBizBean>();
			for (AdvertiseInfo advertiseInfo : advertiseInfos) {
				if(null != advertiseInfo){
					AdvertiseBizBean advertiseBizBean2 = new AdvertiseBizBean();
					PropertyUtils.copyProperties(advertiseBizBean2, advertiseInfo);
					advertiseBizBeans.add(advertiseBizBean2);
				}
			}
			if(null != advertiseBizBeans && advertiseBizBeans.size() > 0){
				return advertiseBizBeans;
			}
		}
		return null;
	}

	@Override
	public Object doExecute(Object obj, String command) throws SystemException,
			ApplicationException {
		// 增加
		if(COMMAND_ADD.equals(command)){
			 return add((AdvertiseAction)obj);
		} else if(COMMAND_DELETE_ALL.equals(command)){
		// 删除
			Map<String, Object> map = (Map<String, Object>)obj;
			if(null != map){
				List<AdvertiseBizBean> advertiseBizBeans = (List<AdvertiseBizBean>)map.get("advertiseBizBeans");
				AdvertiseAction advertiseAction = (AdvertiseAction)map.get("advertiseAction");
				delete(advertiseBizBeans, advertiseAction);
			}
		}
		return null;
	}

	/**
	 * 删除广告
	 * @param obj
	 */
	private void delete(List<AdvertiseBizBean> obj,AdvertiseAction vo) {
		if(null != obj && obj.size() > 0 && vo.getAdvertiseBizBean() != null && vo.getAdvertiseBizBean().getRequest() != null){
			for (AdvertiseBizBean advertiseBizBean : obj) {
				if(null != advertiseBizBean){
					AdvertiseInfo advertiseInfo = advertiseDao.getEntityById(AdvertiseInfo.class, advertiseBizBean.getId());
					if(null != advertiseInfo){
						String myUploadDir = vo.getAdvertiseBizBean().getRequest().getSession().getServletContext().getRealPath("/");
						File userIdDirFile = new File(myUploadDir+"/"+advertiseInfo.getImagePath());
						if(userIdDirFile.exists()|| userIdDirFile.isDirectory()){
							log.info("正在删除广告图片----》"+userIdDirFile.getAbsolutePath());
							userIdDirFile.delete();
						}
						advertiseDao.delete(AdvertiseInfo.class,advertiseBizBean.getId());
					}
					
				}
			}
		} else {
			log.info("广告删除失败！");
		}
	}

	/**
	 * 增加广告
	 * @param obj
	 */
	private Boolean add(AdvertiseAction vo) {
		try {
			if(null != vo && vo.getFiles() !=null && vo.getFiles().size() > 0){
				
				AdvertiseInfo advertiseInfo = new AdvertiseInfo();
				if(null != vo.getAdvertiseBizBean()){
					PropertyUtils.copyProperties(advertiseInfo, vo.getAdvertiseBizBean());
					advertiseDao.save(advertiseInfo);
				}
				
				// 取得上传文件类型
				vo.setFileAllowedTypes(Constants.UPDATE_FILE_TYPES);
				// 取得上传文件大小
				vo.setFileMaxSize(Long.valueOf(Constants.FILE_SIZE));
				// 图片上传的目的地
				String myUploadDir = vo.getAdvertiseBizBean().getRequest().getSession().getServletContext().getRealPath("/advertiseImage");
				// Constants.IMAGE_UPLOAD_PATH;
				File userIdDirFile = new File(myUploadDir);
				if(!(userIdDirFile.exists())&&!(userIdDirFile.isDirectory())){
		           boolean creadok = userIdDirFile.mkdirs();
		           if (creadok) {
		        	   myUploadDir=myUploadDir+"/";
		           }
		        }else{
		        	myUploadDir=myUploadDir+"/";
		        }
				
				String newDir = DateUtils.formatDate(new Date(), "yyyyMMdd");
				File dirFile = new File(myUploadDir+"/"+newDir);
		        if(!(dirFile.exists())&&!(dirFile.isDirectory())){
		           boolean creadok = dirFile.mkdirs();
		           if (creadok) {
		        	   myUploadDir=myUploadDir+"/"+newDir;
		           }
		        }else{
		        	myUploadDir=myUploadDir+"/"+newDir;
		        }
		        vo.setUploadDir(myUploadDir);
				List<FileBean> fileBeanList = vo.handleUploadFile();
				for (FileBean fileBean : fileBeanList) {
					if(null != fileBean){
						String fileName = fileBean.getNewName();
						log.info("正在上传的广告名称为：  "+fileName);
						// 上传路径
						advertiseInfo.setImagePath("/advertiseImage/"+newDir+"/"+fileName);
					}
				}
			} else {
				return false;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
